package com.egzosn.service.pay;


import com.egzosn.entity.pay.PayAccount;
import com.egzosn.pay.common.bean.result.PayException;
import com.egzosn.pay.common.exception.PayErrorException;
import com.egzosn.repository.pay.PayAccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by egan on 15-5-25.
 */
@Service
public class PayApplyService {


    @Autowired
    private PayAccountRepository dao;
    @Autowired
    private AutowireCapableBeanFactory spring;

    private final static Map<Integer, PayResponse> payResponses = new HashMap<Integer, PayResponse>();

    @Transactional
    public PayResponse getPayResponse(Integer payId) {

        PayResponse mpResponse = payResponses.get(payId);
        if (mpResponse == null) {
            PayAccount apyAccount = dao.get(payId);
            if (apyAccount == null) {
                throw  new PayErrorException( new PayException("PayAccount error", "未找到商户无法查询"));
            }
            mpResponse = new PayResponse();
            spring.autowireBean(mpResponse);
            mpResponse.init(apyAccount);
            payResponses.put(payId, mpResponse);
            // 查询
        }
        return mpResponse;
    }


}
